package com.framework.common.sms;

import com.framework.common.sms.interfaces.IAdvanceSmsService;
import com.framework.common.utils.QuickHelper;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TencentCloudSmsService implements IAdvanceSmsService<SmsResponse,SmsServiceConfig> {

    Credential credent;
    SmsClient client;
    SmsServiceConfig config;

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void init(SmsServiceConfig smsServiceConfig) {

        this.config = smsServiceConfig;
        String secretId = smsServiceConfig.getSecretId();
        String secretKey = smsServiceConfig.getSecretKey();
        //cred = new Credential("AKIDqsgGYay6SXu3vWnjw4wKUP3D2LPfrgKT", "NVQBG9m9csraZWGjwztXvJsEPKfBRqlo");
        credent = new Credential(secretId,secretKey);
        HttpProfile profile = new HttpProfile();
        profile.setEndpoint("sms.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(profile);
        client = new SmsClient(credent, "", clientProfile);


        logger.info(String.format("init config : %s", QuickHelper.object2json(smsServiceConfig)));
    }

    @Override
    public SmsResponse send(String mobs, Map<String, String> values) {
        return send(mobs,values,this.config.getDefaultMessageTemplateId());
    }

    @Override
    public SmsResponse send(String mobs, Map<String, String> values, String templateId) {
        String str;
        SmsResponse res = new SmsResponse();
        res.setState(SmsResponse.State.Fail);

        try{
            SendSmsRequest req = new SendSmsRequest();
            String[] mobsArray = mobs.contains(",") ? mobs.split(",") : new String[]{mobs};
            for (int i = 0; i <mobsArray.length ; i++) {
                if(mobsArray[i].length()==11){
                    mobsArray[i] = "+86" + mobsArray[i];
                }
            }
            req.setPhoneNumberSet(mobsArray);
            req.setTemplateID(templateId);
            List<String> lists = new ArrayList<String>(values.values());

            String[] templateValueArray = lists.toArray(new String[]{});
            req.setTemplateParamSet(templateValueArray);

            //req.setSign("杭州铭连网络科技有限公司");
            req.setSign(this.config.getSignName());
            req.setTemplateID(templateId);
            //req.setSmsSdkAppid("1400381752");
            req.setSmsSdkAppid(this.config.getSdkAppid());
            SendSmsResponse rawRes = this.client.SendSms(req);
            str = SendSmsResponse.toJsonString(rawRes);


            //判断发送是否成功
            if(rawRes.getSendStatusSet().length>0 && rawRes.getSendStatusSet()[0].getCode().equals("Ok")){
                res.setState(SmsResponse.State.Success);
            }
            res.setRaw(str);
            res.setRawResponseObject(rawRes);

            logger.info(String.format("send sms response : %s" , QuickHelper.object2json(res)));
            //logger.info(String.format("send sms response : [{}]" , res));

        } catch (TencentCloudSDKException e) {
            logger.error(String.format("send sms error : %s", QuickHelper.object2json(e)));
        }
        return res;
    }
}
